package com.google.code.hbt.servlet;

import java.io.IOException;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch.ricosoft.hr.ResultParser;

@SuppressWarnings("serial")
public class CompleteTableExtractorServlet extends HttpServlet {
	private final static Logger LOG = Logger
			.getLogger(CompleteTableExtractorServlet.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		LOG.entering(getClass().getName(), "doGet");

		String gruppe = req.getParameter("gruppe");
		if (gruppe == null) {
			gruppe = "5164";
		}

		String saison = req.getParameter("saison");
		if (saison == null) {
			saison = "2010";
		}

		ResultParser rp = new ResultParser();
		Calendar fromC = Calendar.getInstance();
		fromC.set(Calendar.YEAR, 2000);
		fromC.set(Calendar.MONTH, Calendar.SEPTEMBER);
		fromC.set(Calendar.DATE, 1);

		Calendar toC = Calendar.getInstance();
		toC.add(Calendar.DATE, 1);

		PrintStream ps = new PrintStream(resp.getOutputStream());

		LOG.info(MessageFormat.format(
				"Calling parser with {0} - {1}, {2}, {3}", fromC, toC, gruppe,
				saison));

		rp.parse(fromC.getTime(), toC.getTime(), gruppe, saison, ps);

		ps.close();
	}

}
